import os
import json
from lib.utils.display import Display
from lib.utils.read_settings import AnsibleSettings as AS

display = Display()
AS = AS()


class Format(object):
    @property
    def combine(self):
        all_facts = []
        try:
            for root, dirs, files in os.walk(AS.cache_base_facts):
                for each_file in files:
                    each_fact = {}
                    each_fact['data'] = {}
                    each_fact['IP'] = each_file.split('.json')[0]
                    each_fact['unreachable'] = self.read_json_file(
                      os.path.join(root, each_file)
                    ).get('unreachable', '')
                    each_fact['data']['base_facts'] = self.read_json_file(
                      os.path.join(root, each_file)
                    ).get('base_facts', '')

                    if os.path.isfile(os.path.join(AS.cache_extra_facts, each_file)):
                        each_fact['data']['extra_facts'] = self.read_json_file(
                          os.path.join(AS.cache_extra_facts, each_file)
                        ).get('extra_facts', '')
                    else:
                        each_fact['data']['extra_facts'] = ''
                    # each_fact['data']['core_facts'] = self.format_core_facts(each_fact['data'])
                    each_fact = self.format_core_facts(each_fact)
                    all_facts.append(each_fact)
                    # print json.dumps(each_fact['core_facts'], indent=4, sort_keys=True)
        except Exception as e:
            display.error('Format data [Fatal Error] on combine: %s' % e)
        finally:
            return json.dumps(all_facts, indent=4)

    def read_json_file(self, file):
        try:
            file_fact = open(file, "r")
            data = json.loads(file_fact.read())
            return data
        except Exception as e:
            return e

    def format_core_facts(self, data):
        data['data']['core_facts'] = {}
        core_facts = data['data']['core_facts']
        base_facts = data.get('data').get('base_facts').get('ansible_facts')
        extra_facts = data.get('data').get('extra_facts').get('linux_facts')
 
        core_facts['ip'] = base_facts.get('ansible_default_ipv4', '').get('address', '')
        core_facts['hostname'] = base_facts.get('ansible_hostname')
        system = base_facts.get('ansible_system')
        core_facts['system'] = system
        core_facts['distribution'] = base_facts.get('ansible_distribution')
        core_facts['distribution_version'] = base_facts.get('ansible_distribution_version')
        core_facts['kernel'] = base_facts.get('ansible_kernel')

        memory = base_facts.get('ansible_memtotal_mb')
        memory = round(float(float(memory)/1000))
        core_facts['memory'] = memory

        core_facts['gateway'] = base_facts.get('ansible_default_ipv4', '').get('gateway', '')
        core_facts['processor'] = base_facts.get('ansible_processor', '')[2]
        core_facts['mac'] = base_facts.get('ansible_default_ipv4', '').get('macaddress', '')
        core_facts['platform'] = base_facts.get('ansible_product_name', '')
        core_facts['python_version'] = base_facts.get('ansible_python_version', '')
        core_facts['architecture'] = base_facts.get('ansible_architecture', '')

        processor_cores = base_facts.get('ansible_processor_cores', '0')
        processor_count = base_facts.get('ansible_processor_count', '0')
        if processor_cores !=0 and processor_count !=0:
            core_facts['vcpus'] = processor_cores * processor_count

        core_facts['architecture'] = base_facts.get('ansible_architecture', '')
        core_facts['default_ipv4_mtu'] = base_facts.get('ansible_default_ipv4', '').get('mtu', '')
        core_facts['mysql_version'] = extra_facts.get('mysql_version', '')
        core_facts['jboss_version'] = extra_facts.get('jboss_version', '')
        core_facts['sqlplus_version'] = extra_facts.get('sqlplus_version', '')

        return data


def main():
    test_Format = Format()
    test_Format.combine
    

if __name__ == '__main__':
    main()
